package com.qiyi.player.wonder.body.model.actors.parts
{
   import flash.net.URLLoader;
   import flash.net.URLRequest;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.wonder.common.config.SystemConfig;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import gs.TweenLite;
   import com.adobe.serialization.json.JSON;
   import com.qiyi.player.base.logging.Log;
   
   public class AlbumInfo extends Object
   {
      
      public function AlbumInfo() {
         this._log = Log.getLogger("com.qiyi.player.wonder.body.model.actors.parts.AlbumInfo");
         super();
      }
      
      private var _loader:URLLoader;
      
      private var _request:URLRequest;
      
      private var _albumID:String = "";
      
      private var _isReady:Boolean = false;
      
      private var _log:ILogger;
      
      private var _albumInfoJSON:Object;
      
      public function get isReady() : Boolean {
         return this._isReady;
      }
      
      public function startLoad(param1:String) : void {
         var _loc2_:String = null;
         if((param1) && !(param1 == this._albumID))
         {
            this.destroyLoader();
            this._isReady = false;
            this._albumID = param1;
            _loc2_ = SystemConfig.ALBUM_INFO_URL + param1;
            this._request = new URLRequest(_loc2_);
            this._loader = new URLLoader();
            this._loader.addEventListener(Event.COMPLETE,this.onCompleteHandler);
            this._loader.addEventListener(IOErrorEvent.IO_ERROR,this.onErrorHander);
            this._loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onErrorHander);
            this._loader.load(this._request);
            this._log.info("albumInfo start request json data : " + this._albumID);
            TweenLite.delayedCall(2,this.delayedRequest);
         }
      }
      
      private function delayedRequest() : void {
         if(this._isReady)
         {
            return;
         }
         this.destroyLoader();
         this._isReady = false;
         var _loc1_:String = SystemConfig.ALBUM_INFO_URL + this._albumID;
         this._request = new URLRequest(_loc1_);
         this._loader = new URLLoader();
         this._loader.addEventListener(Event.COMPLETE,this.onCompleteHandler);
         this._loader.addEventListener(IOErrorEvent.IO_ERROR,this.onErrorHander);
         this._loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onErrorHander);
         this._loader.load(this._request);
         this._log.info("albumInfo delayed request json data : " + this._albumID);
      }
      
      private function onCompleteHandler(param1:Event) : void {
         var event:Event = param1;
         this._isReady = true;
         var startIndex:uint = String(this._loader.data).indexOf("{");
         var jsonStr:String = String(this._loader.data).substring(startIndex);
         try
         {
            this._albumInfoJSON = com.adobe.serialization.json.JSON.decode(jsonStr);
            this._log.info("albumInfo respond json data : " + this._albumID);
         }
         catch(e:Error)
         {
            _albumInfoJSON = null;
            _log.debug("albumInfo json data analyze fail : " + _albumID);
         }
      }
      
      private function onErrorHander(param1:*) : void {
      }
      
      private function destroyLoader() : void {
         if(this._loader)
         {
            this._loader.removeEventListener(Event.COMPLETE,this.onCompleteHandler);
            this._loader.removeEventListener(IOErrorEvent.IO_ERROR,this.onErrorHander);
            this._loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onErrorHander);
            try
            {
               this._loader.close();
            }
            catch(e:Error)
            {
            }
            this._request = null;
            this._loader = null;
         }
      }
      
      public function get albumInfoJSON() : Object {
         return this._albumInfoJSON?this._albumInfoJSON:null;
      }
   }
}
